//
//  AppDelegate+RCConfig.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/16.
//

#define k_RC_Key

#import "AppDelegate+RCConfig.h"
#import "HNSH_SendRedContent.h"
#import "HNSH_OpenContent.h"

@implementation AppDelegate (RCConfig)

//融云注册
- (void)initRongIM {
    //初始化融云SDK
    //正式：4z3hlwrv467vt
    //测试：pwe86ga5pj2b6
    [[RCIM sharedRCIM] initWithAppKey:k_RY_Key];
    //登录状态的监听
    [[RCIM sharedRCIM] setConnectionStatusDelegate:self];
    //设置接收消息代理
    [[RCIM sharedRCIM] setReceiveMessageDelegate:self];
    //头像显示形状
    RCKitConfigCenter.ui.globalMessageAvatarStyle = RC_USER_AVATAR_CYCLE;
    //开启发送已读回执 仅仅单聊
    RCKitConfigCenter.message.enabledReadReceiptConversationTypeList = @[@(ConversationType_PRIVATE),@(ConversationType_DISCUSSION),@(ConversationType_GROUP)];
    //关闭消息引
    RCKitConfigCenter.message.enableMessageReference = NO;
    //开启消息@功能（只支持群聊和讨论组, App需要实现群成员数据源groupMemberDataSource）
    RCKitConfigCenter.message.enableMessageMentioned = YES;
    //开启消息撤回功能
    RCKitConfigCenter.message.enableMessageRecall = YES;
    //消息可撤回的最大时间
    RCKitConfigCenter.message.maxRecallDuration = 120;
    //是否开启视频消息默认不开启
    RCKitConfigCenter.message.isMediaSelectorContainVideo = YES;
    //是否开启转发默认不开启
    RCKitConfigCenter.message.enableSendCombineMessage = YES;
    //开启高清语音
//    [RCIMClient sharedRCIMClient].voiceMsgType = RCVoiceMessageTypeHighQuality;
//    [RCIM sharedRCIM].userInfoDataSource = RCDDataSource;
//    [RCIM sharedRCIM].groupUserInfoDataSource = RCDDataSource;
//    [RCIM sharedRCIM].groupInfoDataSource = RCDDataSource;
//    [RCIM sharedRCIM].groupMemberDataSource = RCDDataSource;
    //持久化本地数据
    [RCIM sharedRCIM].enablePersistentUserInfoCache = YES;
        
    //注册自定义消息
    //发送红包
    [[RCIM sharedRCIM] registerMessageType:[HNSH_SendRedContent class]];
    //打开红包
    [[RCIM sharedRCIM] registerMessageType:[HNSH_OpenContent class]];
//    [[RCIM sharedRCIM] registerMessageType:[DLWantBuyContent class]];
//    [[RCIM sharedRCIM] registerMessageType:[DLSendOrderContent class]];
//    [[RCIM sharedRCIM] registerMessageType:[DLRejectOrderContent class]];
//    [[RCIM sharedRCIM] registerMessageType:[DLConfirmOrderContent class]];
//    [[RCIM sharedRCIM] registerMessageType:[DLGroupNotifyContent class]];
//
//    [[RCIM sharedRCIM] registerMessageType:[NYSendContent class]];
//    [[RCIM sharedRCIM] registerMessageType:[NYOpenContent class]];
}

- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
    NSLog(@"===================%ld", status);
    //如果连接失败 有可能是开发环境上限了（最多100人）
    if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) {
        [[RCIMClient sharedRCIMClient] disconnect];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的帐号已在别的设备上登录，\n您被迫下线！" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [ST_UserManager gotoLogin];
        }]];
        [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:alert animated:YES completion:nil];
    } else if (status == ConnectionStatus_DISCONN_EXCEPTION) {
        [[RCIMClient sharedRCIMClient] disconnect];
    }
   
}

@end
